import {
  Input,
  Radio,
  Extend,
  Select,
  SelectGroup4var,
  RichText,
  TextData
} from '@/components/CustomPage/fields/widget'
import { deepClone } from '@/utils'
export function DataOption4RichText(n, f, t = 'video') {
  return [
    {
      title: '数据设置',
      fields: [
        {
          type: Radio,
          option: {
            label: '数据来源',
            name: 'dataorigin',
            required: true,
            visible: true,
            value: '1',
            list: [
              { name: '固定数据', value: '1' },
              { name: '动态数据', value: '2' }
            ],
            override: function(ths, e) {
              ths.$nextTick(() => {
                if (e === '1') {
                  ths.$parent.$parent.$children[1].visible = true
                  ths.$parent.$parent.$children[2].visible = false
                  ths.$parent.$parent.$children[2].$children[0].show = false
                } else {
                  ths.$parent.$parent.$children[1].visible = false
                  ths.$parent.$parent.$children[2].visible = true
                  ths.$parent.$parent.$children[2].$children[0].show = true
                }
              })
            }
          }
        },
        {
          type: t === 'video' ? RichText : TextData,
          option: {
            portName: n,
            field: f,
            label: '数据配置',
            name: 'fixedData',
            required: true,
            value: '',
            visible: true
          }
        },
        {
          type: Extend,
          option: {
            label: '接口配置',
            name: 'dynamicData',
            visible: false,
            initalize: false,
            disabled: true,
            showRemove: false,
            fields: [
              {
                type: Input,
                option: {
                  label: '配置名称',
                  name: 'configName',
                  disabled: true,
                  required: true,
                  value: ''
                }
              },
              {
                type: Input,
                option: {
                  label: '配置标识',
                  name: 'configCode',
                  disabled: true,
                  required: false,
                  value: ''
                }
              },
              {
                type: Select,
                option: {
                  label: '选择接口',
                  name: 'choosePort',
                  required: true,
                  value: '',
                  async: 'backend/pageRegister/queryRegByComId',
                  asyncParams: { comId: '' },
                  override: function(ths, e, arr = []) {
                    ths.$nextTick(() => {
                      if (arr.length === 0) {
                        return false
                      }
                      const obj = arr.find((item) => {
                        return e === item.value
                      })
                      if (obj === undefined) {
                        return false
                      }
                      const childs = ths.$parent.$parent.$children
                      childs.forEach((item, index) => {
                        if (item.option.name === 'name') {
                          ths.$parent.$parent.$children[index].setValue(
                            obj.name
                          )
                        } else if (item.option.name === 'link') {
                          ths.$parent.$parent.$children[index].setValue(
                            obj.pageRoute
                          )
                        } else if (item.option.name === 'params') {
                          ths.$parent.$parent.$children[
                            index
                          ].$children[1].clear()
                          let param = []
                          try {
                            param = JSON.parse(obj.pageParam)
                          } catch (e) {
                            param = []
                          }
                          param.forEach((item) => {
                            const arr = deepClone(
                              ths.$parent.$parent.$children[index].$children[1]
                                .fields
                            )
                            arr[0].option.value = item.keyCode
                              ? item.keyCode
                              : ''
                            ths.$parent.$parent.$children[
                              index
                            ].$children[1].add(arr)
                          })
                          ths.$parent.$parent.$children[
                            index
                          ].$children[1].disabled_ = true
                          ths.$parent.$parent.$children[
                            index
                          ].$children[1].showRemove = false
                        }
                      })
                    })
                  }
                }
              },
              {
                type: Input,
                option: {
                  label: '接口名称',
                  name: 'name',
                  disabled: true,
                  required: true,
                  value: ''
                }
              },
              {
                type: Input,
                option: {
                  label: '接口地址',
                  name: 'link',
                  disabled: true,
                  required: true,
                  value: ''
                }
              },
              {
                type: Extend,
                option: {
                  label: '接口参数',
                  name: 'params',
                  initalize: false,
                  disabled: true,
                  showRemove: false,
                  fields: [
                    {
                      type: Input,
                      option: {
                        label: '参数名',
                        name: 'key',
                        disabled: true,
                        value: '',
                        required: true
                      }
                    },
                    {
                      type: Radio,
                      option: {
                        label: '参数值类型',
                        name: 'paramType',
                        required: true,
                        value: '1',
                        list: [
                          { name: '固定参数', value: '1' },
                          { name: '选择变量', value: '2' }
                        ],
                        override: function(ths, e) {
                          ths.$nextTick(() => {
                            if (e === '1') {
                              // 只显示参数值
                              ths.$parent.$parent.$children[2].visible = true
                              ths.$parent.$parent.$children[3].visible = false
                              ths.$parent.$parent.$children[4].visible = false
                            } else {
                              ths.$parent.$parent.$children[2].visible = false
                              ths.$parent.$parent.$children[3].visible = true
                              ths.$parent.$parent.$children[4].visible = true
                            }
                          })
                        }
                      }
                    },
                    {
                      type: Input,
                      option: {
                        label: '参数值',
                        name: 'fixedParam',
                        value: ''
                      }
                    },
                    {
                      type: SelectGroup4var,
                      option: {
                        visible: false,
                        label: '变量初始值',
                        name: 'dynamicParam',
                        value: ''
                      }
                    },
                    {
                      type: SelectGroup4var,
                      option: {
                        visible: false,
                        label: '组件关联变量',
                        name: 'compVar',
                        value: ''
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  ]
}
